// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <pollux/common/memory/hash_string_allocator.h>

namespace kumo::pollux::aggregate::prestosql {
    /// Stores non-inlined strings in memory blocks managed by
    /// HashStringAllocator.
    struct Strings {
        /// Header of the first block allocated via HashStringAllocator.
        HashStringAllocator::Header *firstBlock{nullptr};

        /// Header of the last block allocated via HashStringAllocator. Blocks
        /// are forming a singly-linked list via Header::nextContinued.
        /// HashStringAllocator takes care of linking the blocks.
        HashStringAllocator::Position currentBlock{nullptr, nullptr};

        /// Maximum size of a string passed to 'append'. Used to calculate the amount
        /// of space to reserve for future strings.
        size_t maxStringSize = 0;

        /// Copies the string into contiguous memory allocated via
        /// HashStringAllocator. Returns StringView over the copy.
        StringView append(StringView value, HashStringAllocator &allocator);

        /// Frees memory used by the strings. StringViews returned from 'append'
        /// become invalid after this call.
        void free(HashStringAllocator &allocator);
    };
} // namespace kumo::pollux::aggregate::prestosql
